package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.Role;
import com.jsz.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //添加角色信息
    @PreAuthorize("hasAuthority('ROLE_ADD')")//添加角色信息权限
    @RequestMapping("/addRole/{permissionIds}")
    public Result addRole(@RequestBody Role role, @PathVariable Integer[] permissionIds) {
        try {
            roleService.addRole(role, permissionIds);
            //添加数据成功
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加数据失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    //根据ID查询
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")//查询角色信息权限
    public Result findById(@PathVariable Integer id) {
        try {
            Role role = roleService.findById(id);

            System.out.println("role = " + role);

            //查询成功
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色ID查询中间关系表
    @PreAuthorize("hasAuthority('ROLE_QUERY')")//查询角色信息权限
    @RequestMapping("/findPermissionIdsByRoleId/{id}")
    public Result findPermissionIdsByRoleId(@PathVariable Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //修改角色信息以及关联的权限
    @RequestMapping("/update/{permissionIds}")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")//修改角色信息权限
    public Result update(@RequestBody Role role, @PathVariable Integer[] permissionIds) {
        try {
            roleService.update(role, permissionIds);
            //修改数据成功
            return new Result(true, MessageConstant.UPDATE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改数据失败
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        }
    }

    //删除角色信息
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")//删除角色信息权限
    public Result deleteById(Integer id) {
        //调试
        System.out.println("id = " + id);

        try {
            roleService.deleteById(id);
            //删除数据成功
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除数据失败
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //查询所有角色信息
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            //查询成功
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            //查询失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
